package com.ruoyi.common.google;

import com.google.api.client.googleapis.auth.oauth2.*;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.Map;

@Service
public class GoogleAuthServie {

//    @Autowired
//    private GoogleAuthorization googleAuthorization;

    /*浏览器访问这个url，跳转到同意屏幕*/
//    public String authorizingUrl() throws GeneralSecurityException, IOException {
//        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
//        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
//        // 创建验证流程对象
//        GoogleAuthorizationCodeFlow googleAuthorizationCodeFlow = new GoogleAuthorizationCodeFlow
//                .Builder(httpTransport, jsonFactory, googleAuthorization.getGoogleClientSecrets(), googleAuthorization.getScopes())
//                // AccessType为离线offline，才能获得Refresh Token
//                .setAccessType("offline").build();
//        if (googleAuthorizationCodeFlow != null) {
//            // 返回跳转登录请求
//            return googleAuthorizationCodeFlow.newAuthorizationUrl().setRedirectUri(googleAuthorization.getRedirectUrl()).build();
//        }
//        return null;
//    }
//
//    //使用授权码获取账号token refreshToken email
//    public Map<String,String> authorizing(String  authorizationCode) throws GeneralSecurityException, IOException {
//        Map<String,String> map = new HashMap<>();
//        // 创建请求凭证
//        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
//        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
//        GoogleAuthorizationCodeFlow googleAuthorizationCodeFlow = new GoogleAuthorizationCodeFlow
//                .Builder(httpTransport, jsonFactory, googleAuthorization.getGoogleClientSecrets(), googleAuthorization.getScopes())
//                // AccessType为离线offline，才能获得Refresh Token
//                .setAccessType("offline").build();
//        GoogleAuthorizationCodeTokenRequest tokenRequest = googleAuthorizationCodeFlow.newTokenRequest(authorizationCode);
//        tokenRequest.setRedirectUri(googleAuthorization.getRedirectUrl());
//        // 发起授权请求，获得Token和Refresh Token
//        GoogleTokenResponse tokenResponse = tokenRequest.execute();
//        String token = tokenResponse.getAccessToken();
//        String refreshToken = tokenResponse.getRefreshToken();
//        map.put("token",token);
//        map.put("refreshToken",refreshToken);
//        // 获得email
//        String email = null;
//        if (StringUtils.isNotBlank(tokenResponse.getIdToken())) {
//            GoogleIdTokenVerifier idTokenVerifier = new GoogleIdTokenVerifier.Builder(googleAuthorizationCodeFlow.getTransport(), googleAuthorizationCodeFlow.getJsonFactory()).build();
//            idTokenVerifier.verify(tokenResponse.getIdToken());
//            GoogleIdToken googleIdToken = idTokenVerifier.verify(tokenResponse.getIdToken());
//            if (googleIdToken != null && googleIdToken.getPayload() != null) {
//                email = googleIdToken.getPayload().getEmail();
//                map.put("email",email);
//            }
//        }
//       return map;
//
//        // todo 保留账号token、refreshToken、email信息
//    }
//
//
//    //根据refreshToken  获取最新token
//    public String refreshToken(String refreshToken) throws IOException {
//        String token = null;
//        // 创建刷新请求对象
//        GoogleRefreshTokenRequest googleRefreshTokenRequest = new GoogleRefreshTokenRequest(
//                new NetHttpTransport(),
//                JacksonFactory.getDefaultInstance(),
//                refreshToken,
//                googleAuthorization.getGoogleClientSecrets().getDetails().getClientId(),
//                googleAuthorization.getGoogleClientSecrets().getDetails().getClientSecret());
//        // 发起刷新请求
//        GoogleTokenResponse googleTokenResponse = googleRefreshTokenRequest.execute();
//        if (googleTokenResponse != null && StringUtils.isNotBlank(googleTokenResponse.getAccessToken())) {
//            token = googleTokenResponse.getAccessToken();
//        }
//        return null;
//    }
}
